package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.dto.UserPageDTO;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.mapper.UserMapper;
import cn.kgc.ssm.response.ResponseResult;
import cn.kgc.ssm.service.UserService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/11/14
 * @description
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult login(String username, String password, String code, HttpServletRequest request) {

        //  比较验证码
        String sessionCode = (String) request.getSession().getAttribute("code");
        if (!sessionCode.equals(code)) {
            return ResponseResult.fail().msg("验证码错误");
        }

        // 根据用户名查询用户信息
        User user = userMapper.selectUserByUserName(username);
        if (ObjectUtils.isEmpty(user)) {
            return ResponseResult.fail().msg("用户名错误");
        }

        // 比较密码
        String dbPassword = user.getPassword();
        String formPassword = SecureUtil.md5(password);
        if (!StringUtils.equals(dbPassword, formPassword)) {
            return ResponseResult.fail().msg("密码错误");
        }

        //  保存用户信息在session
        request.getSession().setAttribute("user", user);
        // authentication  用于后续非法请求的拦截
        request.getSession().setAttribute("authentication", "true");

        // 校验通过   获取用户的昵称  返回前端
        String nickname = user.getNickname();

        return ResponseResult.success().msg("认证通过").data(nickname);
    }

    @Override
    public ResponseResult personalInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();

        User usr = userMapper.selectUserByUserName(username);
        return ResponseResult.success().data(usr);
    }

    @Override
    public ResponseResult editPersonalInfo(User user) {


        userMapper.editPersonalInfo(user);

        return ResponseResult.success().msg("个人信息编辑成功");
    }

    @Override
    public ResponseResult editPersonalPwd(String oldPassword, String newPassword, HttpSession session) {

        // 判定用户的旧密码是否正确
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        User usr = userMapper.selectUserByUserName(username);
        String dbPassword = usr.getPassword();
        String formPassword = SecureUtil.md5(oldPassword);

        if (!StringUtils.equals(dbPassword, formPassword)) {
            return ResponseResult.fail().msg("原始密码不正确");
        }

        String newDBPassword = SecureUtil.md5(newPassword);

        userMapper.editPersonalPwd(usr.getId(), newDBPassword);

        return ResponseResult.success().msg("密码编辑成功");
    }

    @Override
    public ResponseResult userPage(UserPageDTO userPageDTO) {

        //  分页查询
        PageHelper.startPage(userPageDTO.getPage(), userPageDTO.getLimit());

        List<User> tableData = userMapper.selectUserPage(userPageDTO.getAge(), userPageDTO.getUsername());

        PageInfo<User> userPageInfo = new PageInfo<>(tableData);
        long total = userPageInfo.getTotal();

        return ResponseResult.success().count(total).data(tableData);
    }

    @Override
    public ResponseResult addUser(User user) {

        //  判定用户名是否可用
        String username = user.getUsername();
        User usr = userMapper.selectUserByUserName(username);
        if (!ObjectUtils.isEmpty(usr)){
            return  ResponseResult.fail().msg("用户名已被占用");
        }

        // 添加数据到user表   default password
        String defaultPassword = "111111";
        user.setPassword(SecureUtil.md5(defaultPassword));

        userMapper.addUser(user);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult editUser(User user) {

        userMapper.updateUser(user);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult deleteUser(List<Integer> ids) {

        userMapper.batchDelete(ids);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult resetPassword(Integer id) {


        userMapper.resetPwd(id,SecureUtil.md5("111111"));

        return ResponseResult.success();
    }
}
